package com.jboltai.event.embedding;

import cn.hutool.core.util.StrUtil;
import com.jboltai.capability.embedding.EmbeddingProcessor;
import com.jboltai.event.Event;
import com.jboltai.event.EventAttrKey;
import com.jboltai.event.EventType;
import com.jboltai.event.handler.EmbeddingEventHandler;
import com.jboltai.resource.ai.AIResource;
import com.jboltai.resource.embedding.EmbeddingModel;

import java.util.List;

/**
 * 文本向量化事件
 */
public class EmbeddingEvent extends Event<EmbeddingEvent, List<List<Double>>, EmbeddingEventHandler> {
    public EmbeddingEvent() {
        super(EventType.EMBEDDING);
    }

    public EmbeddingEvent(EmbeddingModel embeddingModel, String text) {
        this(embeddingModel, new String[]{text});
    }


    public EmbeddingEvent(EmbeddingModel embeddingModel, List<String> textList) {
        this(embeddingModel, textList == null ? null : textList.toArray(new String[0]));
    }

    /**
     * 构建Embedding事件
     * @param embeddingModel 指定要使用的模型，因为embedding 各模型不通用，所以必须指定用哪个
     * @param text 文本
     */
    public EmbeddingEvent(EmbeddingModel embeddingModel, String[] text) {
        super(EventType.EMBEDDING);
        this.text = text;
        this.embeddingModel = embeddingModel;
    }
    public EmbeddingEvent(String embeddingResourceId, String text) {
        this(embeddingResourceId, new String[]{text});
    }


    public EmbeddingEvent(String embeddingResourceId, List<String> textList) {
        this(embeddingResourceId, textList == null ? null : textList.toArray(new String[0]));
    }

    /**
     * 构建Embedding事件
     * @param embeddingResourceId 指定要使用的资源id，因为embedding 各模型不通用，所以必须指定用哪个
     * @param text 文本
     */
    public EmbeddingEvent(String embeddingResourceId, String[] text) {
        super(EventType.EMBEDDING);
        this.text = text;
        this.embeddingResourceId = embeddingResourceId;
    }

    private String[] text;

    /**
     * 指定使用的embedding资源id
     */
    private String embeddingResourceId;

    /**
     * 指定使用的embedding模型
     */
    private EmbeddingModel embeddingModel;


    @Override
    public String preCheck() {
        if (text == null || text.length == 0) {
            return "文本内容不能为空";
        }
        for (String s : text) {
            if (StrUtil.isEmpty(s)) {
                return "向量化的文本不能为空字符串";
            }
        }
        if (embeddingModel == null && embeddingResourceId == null) {
            return "未找到可用的Embedding资源，请检查embeddingModel或者embeddingResourceId是否有效";
        }

        return null;
    }

    public String[] getText() {
        return text;
    }

    public EmbeddingEvent setText(String[] text) {
        this.text = text;
        return this;
    }

    public String getEmbeddingResourceId() {
        return embeddingResourceId;
    }

    public EmbeddingEvent setEmbeddingResourceId(String embeddingResourceId) {
        this.embeddingResourceId = embeddingResourceId;
        return this;
    }

    public EmbeddingModel getEmbeddingModel() {
        return embeddingModel;
    }

    public EmbeddingEvent setEmbeddingModel(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
        return this;
    }

    @Override
    protected void innerCancelHandle() {
        EmbeddingProcessor processor = this.getAttr(EventAttrKey.EVENT_PROCESSOR);
        AIResource resource = this.getAttr(EventAttrKey.EVENT_RESOURCE);
        if (processor != null) {
            processor.handleCancel(this, resource);
        }
    }
}
